//
//  NRWCarBottomMenuView.m
//  New_Runway
//
//  Created by NewLife on 2018/2/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarBottomMenuView.h"

@implementation NRWCarBottomMenuView
{
    NSArray *titles;
    NSArray *images;
    
    UIButton *cancelBtn;
    
    UIView *backView;
    
    CGFloat bottomMargin;
}

- (instancetype)initWithTitleArr:(NSArray *)titleArr imageArr:(NSArray *)imageArr
{
    self = [super init];
    if (self) {
        
        if (titleArr.count != imageArr.count) {
            return nil;
        }
        
        bottomMargin = Screen_height == 812? 34: 0;
        
        titles = titleArr;
        images = imageArr;
        
        backView = DPView().setBackgroundColor([UIColor whiteColor]);
        backView.userInteractionEnabled = YES;
        self.addSubview(backView);
        
        [self setupUI];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        self.frame = [UIScreen mainScreen].bounds;
    }
    return self;
}

- (void)setupUI {
    
    for (NSInteger i = 0; i < titles.count; i++) {
        
        UIButton *btn = DPLayoutButton(16, DPBtnLayoutTypeUpImageDownTitle)
        .setNormalImage([UIImage imageNamed:images[i]])
        .setNormalTitle(titles[i])
        .setTitleFont(kPingFangRegular13())
        .setNormalTitleColor([UIColor colorFromHexCode:@"323232"])
        .setTag(i)
        .addClickAction(self, @selector(btnDidClick:));
        
        backView.addSubview(btn);
        
        NSInteger row = i / 3;
        NSInteger col = i % 3;
        NSInteger lrMargin = 50;
        NSInteger topMargin = 32;
        NSInteger btnW = 53;
        NSInteger btnH = 68;
        NSInteger margin = (Screen_width - getRealWidth(100.0 + btnW * 3)) / 2.0;
        
        btn.frame = CGRectMake(getRealWidth(lrMargin) + (margin + getRealWidth(btnW)) * col, topMargin + (topMargin + btnH) * row, btnW, btnH);
        
        if (i == titles.count - 1) {
            
            cancelBtn = DPButton()
            .addClickAction(self, @selector(cancelBtnDidClick:))
            .setFrame(CGRectMake(27.5, btn.bottom + 27, Screen_width - 55, 40))
            .setNormalTitleColor([UIColor colorFromHexCode:@"ff6816"])
            .setTitleFont(kPingFangRegular17())
            .setNormalTitle(@"取    消");
            cancelBtn.layer.cornerRadius = 5;
            cancelBtn.layer.borderWidth = 0.5;
            cancelBtn.layer.borderColor = [UIColor colorFromHexCode:@"ff6816"].CGColor;
            [backView addSubview:cancelBtn];
        }
    }
}

- (void)btnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(bottomMenuView:didClickBtn:)]) {
        
        [self.delegate bottomMenuView:self didClickBtn:sender];
    }
}

- (void)cancelBtnDidClick:(UIButton *)sender {
    
    [self dismiss];
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
    
    if (CGRectContainsPoint(backView.frame, [sender locationInView:self])) {
        return;
    }
    
    [self dismiss];
}

- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    backView.frame = CGRectMake(0, Screen_height, Screen_width, cancelBtn.bottom + 18);
    
    [UIView animateWithDuration:0.25 animations:^{
       backView.frame = CGRectMake(0, Screen_height - cancelBtn.bottom - 18 - bottomMargin, Screen_width, cancelBtn.bottom + 18 + bottomMargin);
        
        if ([self.delegate respondsToSelector:@selector(bottomMenuViewDidAppear:)]) {
            
            [self.delegate bottomMenuViewDidAppear:self];
        }
    }];
}

- (void)dismiss {
    
    [UIView animateWithDuration:0.25 animations:^{
        backView.frame = CGRectMake(0, Screen_height, Screen_width, cancelBtn.bottom + 18 + bottomMargin);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
        if ([self.delegate respondsToSelector:@selector(bottomMenuViewDidDisappear:)]) {
            
            [self.delegate bottomMenuViewDidDisappear:self];
        }
    }];
    
}

@end
